#!/bin/bash
n=1
m=1
live_time=1
dead_time=1
interval_time=1
while :
do
	ftp_server_ip="192.168.0.189"
	log_date=$(date +%F_%H:%M:%S)
	if nc -z $ftp_server_ip 21 &>/dev/null
	then
		echo -e "$log_date\t$ftp_server_ip is up\t$live_time"&&m=1
		if (( n == 1 ))
		then
			echo -e "$log_date\t$ftp_server_ip\tup" >>/var/log/monitor_ftp.log
			((n++))
		fi
		((live_time++))
		sleep 1
	else
		H_time=$((dead_time/3600))
		M_time=$((dead_time%3600/60))
		S_time=$((dead_time%3600%60))
		echo -e  "$log_date\t$ftp_server_ip is down\t ${H_time}:${M_time}:${S_time}"&&n=1
		if (( m == 1 ))
		then
			echo -e "$log_date\t$ftp_server_ip\tdown" >>/var/log/monitor_ftp.log
			((m++))
		fi
		((dead_time++))
		sleep 1
	fi
	if (($interval_time%10 == 0))
	then
		f_time=`cat /var/log/monitor_ftp.log |head -1|awk '{print $1}'`
		e_time=`cat /var/log/monitor_ftp.log |tail -1|awk '{print $1}'`
		down_count=`cat /var/log/monitor_ftp.log |egrep "down"|wc -l`
		echo -e  "\n"
		echo "#########################################################"
		echo -e "the ftp server from $f_time to $e_time has down $down_count times"
		echo "#########################################################"
		echo -e "\n"
	fi
	((interval_time++))
	
done
